package edu.scut.course_management.service;

import java.time.Year;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import edu.scut.course_management.dao.AcademicTitleMapper;
import edu.scut.course_management.dao.BuildingMapper;
import edu.scut.course_management.dao.ClassMapper;
import edu.scut.course_management.dao.ClassroomMapper;
import edu.scut.course_management.dao.CourseTypeMapper;
import edu.scut.course_management.dao.DepartmentMapper;
import edu.scut.course_management.dao.EnrollmentYearMapper;
import edu.scut.course_management.dao.MajorMapper;
import edu.scut.course_management.dto.GetCourseTypeDTO;
import edu.scut.course_management.dto.YearDTO;
import edu.scut.course_management.po.AcademicTitle;
import edu.scut.course_management.po.Department;
import edu.scut.course_management.po.EnrollmentYear;
import edu.scut.course_management.po.Major;
import edu.scut.course_management.po.Class;
import edu.scut.course_management.po.CourseType;

@Service
public class InfomationService {
    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    EnrollmentYearMapper enrollmentYearMapper;
    @Autowired
    MajorMapper majorMapper;
    @Autowired
    AcademicTitleMapper academicTitleMapper;
    @Autowired
    ClassMapper classMapper;
    @Autowired
    BuildingMapper buildingMapper;
    @Autowired
    ClassroomMapper classroomMapper;
    @Autowired
    CourseTypeMapper courseTypeMapper;

    /**
     * 返回院系列表
     */
    public List<Department> getAllDepartments () {
        return departmentMapper.getAllDepartments();
    }

    /**
     * 返回入学年份列表
     */
    public List<EnrollmentYear> getAllEnrollmentYears () {
        return enrollmentYearMapper.getAllEnrollmentYears();
    }
    
    /**
     * 返回院系专业列表
     */
    public List<Major> getDepartmentMajors (Integer departmentId) {
        return majorMapper.getDepartmentMajors(departmentId);
    }

    /**
     * 返回专业班级列表
     */
    public List<Class> getMajorClasses (Integer majorId, String enrollmentYear) {
        return classMapper.getMajorClasses(majorId, enrollmentYear);
    }

    /**
     * 返回职称列表
     */
    public List<AcademicTitle> getAllAcademicTitles () {
        return academicTitleMapper.getAllAcademicTitles();
    }

    /**
     * 返回楼栋列表
     */
    public List<String> getAllBuildingIds(){
        return buildingMapper.findAllBuildingId();
    }

    /**
     * 根据楼栋返回教室列表
     */
    public List<String> getAllRoomNumbers(String buildingId){
        return classroomMapper.findAllRoomNumbersByBuildingId(buildingId);
    }

    /**
     * 返回所有的课程类别列表
     */
    public GetCourseTypeDTO getAllCourseType(){
        List<CourseType> list = courseTypeMapper.findAllCourseType();
        GetCourseTypeDTO getCourseTypeDTO = new GetCourseTypeDTO();
        getCourseTypeDTO.setCourseType(list);
        return getCourseTypeDTO;
    }

    /**
     * 返回学年列表
     */
    public YearDTO getYears(){
        Calendar cal = Calendar.getInstance();
        int base = cal.get(Calendar.YEAR);
        List<String> yearslist = new ArrayList<>();
        yearslist.add(Integer.toString(base-2));
        yearslist.add(Integer.toString(base-1));
        yearslist.add(Integer.toString(base));
        yearslist.add(Integer.toString(base+1));
        yearslist.add(Integer.toString(base+2));
        YearDTO yearDTO = new YearDTO(yearslist);

        return yearDTO;
    }

    public boolean isExistDepartment (Integer departmentId) {
        Department department = departmentMapper.findByDepartmentId(departmentId);
        if (department != null) {
            return true;
        }
        return false;
    }

    public boolean isExistMajor (Integer majorId) {
        Major major = majorMapper.findByMajorId(majorId);
        if (major != null) {
            return true;
        }
        return false;
    }

}
